// pages/home/topic/index.js
import {
  BASE_URL
} from '../../../constant/index'
import {
  $on,
  $off,
  $emit
} from '../../../utlis/eventBus'
import {
  getTopicApi
} from '../../../https/topic'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    BASE_URL,
    classifyList: ['热门', '骑行', '赛事', '打卡', '装备', '康复', '饮食', '其他'], // 分类标签
    classifyIndex: 0,
    isBack: false // 是否会掉参数
      ,
    topicList: [],
    where: {

    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options)
    if (options.isBack) {
      this.setData({
        isBack: true
      })
    }
    this.getTopicList()
  },
  async getTopicList() {
    const {
      where
    } = this.data
    console.log(where)

    const getRes = await getTopicApi(where)
    console.log(getRes)
    if (getRes.code == 200) {
      this.setData({
        topicList: getRes.data
      })
    }
  },
  // 切换轮播图
  toggleSwiper(e) {
    const {
      where,
      classifyList
    } = this.data
    console.log(e)
    const index = e.currentTarget.dataset.index

    if (index === 0) {
      delete where.type
    } else {
      where.type = classifyList[index]
    }
    this.setData({
      classifyIndex: index,
      where
    });

    this.getTopicList()
  },
  
  // 跳转话题详情
  gotoTopicDetail(e) {
    const isBack = this.data.isBack
    console.log(e)
    console.log(isBack)
    const item = e.currentTarget.dataset.item

    if (isBack) {
      $emit("topicEvent", item)
      wx.navigateBack()
      return
    }

    wx.navigateTo({
      url: '../topicDetail/index?docid=' + item.id,
    })
  }
})